package com.example.musictest.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.musictest.R;
import com.example.musictest.databinding.FragmentHomeBinding;
import com.example.musictest.ui.adapter.MusicAdapter;
import com.fw.mediasession.media.bean.Music;
import com.fw.mediasession.media.bean.MyPicture;
import com.fw.mediasession.media.mediastore.inter.GetMediaData;
import com.zy.arouter.ARouterConstants;
import com.zy.base.fragment.BaseFragment;
import com.zy.base.recycleview.BaseRecycleAdapter;
import com.zy.untils.MyLog;

import java.io.File;
import java.util.ArrayList;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;
import io.reactivex.Observer;

/**
 * 文件扫描的
 */
@AndroidEntryPoint
public class HomeFragment extends BaseFragment {


   public HomeViewModel homeViewModel ;
    private FragmentHomeBinding binding;
    private FragmentActivity context;

//    @Inject
//    GetMediaData mediaDataInterface;
    private MusicAdapter adapter;
    private ArrayList<Music> myMusics;


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        binding= DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);

        View root = binding.getRoot();
        homeViewModel =
                new ViewModelProvider(requireActivity()).get(HomeViewModel.class);
        binding.setViewModel(homeViewModel);
        binding.setLifecycleOwner(this);
        initView();

//        test();
        return root;
    }



    private void test() {
        
        File dataDirectory = Environment.getDataDirectory();///data 内部存储
        File rootDirectory = Environment.getRootDirectory();///system
        File externalStorageDirectory = Environment.getExternalStorageDirectory();///storage/emulated/0 外部存储
        String storageState = Environment.getExternalStorageState();//mounted
        File downloadCacheDirectory = Environment.getDownloadCacheDirectory();///data/cache


//        externalStorageDirectory
    }



    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.context=requireActivity();
    }

    private void initView() {
        adapter = new MusicAdapter(requireActivity(),new ArrayList(), R.layout.item_music_recy);
        LinearLayoutManager layoutManager = new LinearLayoutManager(requireActivity());
        binding.musicRecyclerview.setLayoutManager(layoutManager);
        binding.musicRecyclerview.setAdapter(adapter);

        homeViewModel.musics.observe(context, musics -> {
            MyLog.i("数据：musics"+musics.size());
            myMusics = musics;

            adapter.updateData(musics);
        });
        homeViewModel.videos.observe(context, videos -> {
            MyLog.i("数据：videos"+videos.size()) ;
        });
        homeViewModel.pictures.observe(context, pictures ->{
            MyLog.i("数据：pictures"+pictures.size()) ;
        } );

        adapter.setOnItemClickListener(new BaseRecycleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                MyLog.i("点击：music"+position) ;
                homeViewModel.playMediaList(myMusics,position,0);
                Navigation.findNavController(view).navigate(R.id.action_navigation_home_to_navigation_music);
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

}